package shanghai;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

public class HttpSpiderShanghaiJinanLevel2 {
    public static void main(String[] args) throws TimeoutException {


        //String path = System.getProperty("user.dir") + "/doc/43000000000shanghaijingan";
        String path = "C:\\csvshanghaijinan300";
        CsvWriter writer = new CsvWriter("D:\\shanghaiswtjingan3.csv");
        int index = 2;
        for (int i = 201; i < index + 1; i++) {
            String file = path + "/" + i + ".json";
            String content = FileUtil.readUtf8String(file);
            JSONArray objects = JSONUtil.parseArray(content);
            for (int i1 = 0; i1 < objects.size(); i1++) {
                JSONObject jsonObject = objects.getJSONObject(i1);
                String qlcode = jsonObject.getStr("QLCODE");
                String areacode = jsonObject.getStr("AREACODE");
                String qlname = jsonObject.getStr("QLNAME");
                String html = HttpRequest.get("http://zwdt.sh.gov.cn/govPortals/power/powerDetail.do?zr=0&stId=" + qlcode + "&stItemRegion=" + areacode)
                        .execute()
                        .body();


                Document document = Jsoup.parse(html);
                Element body = document.body();
                Elements select = body.select("div[class=item-service]");
                if (select.size() != 0) {
                    List<String> list = new ArrayList<String>();
                    int size = select.size();
                    for (int i2 = 0; i2 < size; i2++) {
                        Element div = select.get(i2);
                        Element select1 = div.select("a").get(0);

                        String onclick = select1.attr("onclick");
                        list.add(onclick);
                        for (int i3 = 0; i3 < list.size(); i3++) {
                            String contents = list.get(i3);
                            System.err.println(contents);
                            String s = StrUtil.sub(onclick, StrUtil.indexOf(onclick, '(') + 1, StrUtil.indexOf(onclick, ')'));
                            String[] str = StrUtil.split(s, ",");


                            String s1 = str[0].replace("'", "");
                            //System.out.println(s1);
                            String s2 = str[1].replace("'", "");
                            //System.out.println(s2);
                            String s3 = str[2].replace("'", "");
                            // System.out.println(s3);
                            String s4 = qlname;
                            System.err.println(s4);
                            String strs = "http://zwdt.sh.gov.cn/govPortals/bsfw/findBsfw.do?_organName_=SH00SH&_organCode_=SH00SH&_organType_=&_itemId=" + s1 + "&_itemType=" + s2 + "&_stSubitemId=";
                            String[] strings = new String[3];
                            strings[0] = s4;
                            strings[1] = s3;
                            strings[2] = strs;
                            writer.write(strings);
                        }

                        ThreadUtil.sleep(1000);

                    }


                }else {
                    String [] strings=new String[3];
                    String s1=qlname;
                    System.out.println("-------测试:------"+ qlname);
                    strings[0]=s1;
                    strings[1]=" ";
                    strings[2]=" ";
                    writer.write(strings);
                }

                ThreadUtil.sleep(1000);
            }
        }
    }
}
